package com.och.mrcp.core.sip.model;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * SIP响应消息
 * RFC 3261 - SIP: Session Initiation Protocol
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class SipResponse extends SipMessage {
    
    private int statusCode;
    private String reasonPhrase;
    private String sipVersion = "SIP/2.0";
    
    public SipResponse() {
        super();
    }
    
    public SipResponse(int statusCode, String reasonPhrase) {
        this();
        this.statusCode = statusCode;
        this.reasonPhrase = reasonPhrase;
        setStartLine();
    }
    
    @Override
    public String getMethod() {
        return null; // 响应消息没有方法
    }
    
    @Override
    public boolean isRequest() {
        return false;
    }
    
    @Override
    public boolean isResponse() {
        return true;
    }
    
    public void setStartLine() {
        this.startLine = sipVersion + " " + statusCode + " " + reasonPhrase;
    }
    
    // RFC 3261 Section 21.1 - 状态码分类
    public boolean isSuccess() {
        return statusCode >= 200 && statusCode < 300;
    }
    
    public boolean isClientError() {
        return statusCode >= 400 && statusCode < 500;
    }
    
    public boolean isServerError() {
        return statusCode >= 500 && statusCode < 600;
    }
    
    public boolean isProvisional() {
        return statusCode >= 100 && statusCode < 200;
    }
    
    public boolean isRedirection() {
        return statusCode >= 300 && statusCode < 400;
    }
    
    public boolean isGlobalFailure() {
        return statusCode >= 600 && statusCode < 700;
    }
    
    // RFC 3261 Section 21.1 - 标准响应状态码
    
    // 1xx Provisional (临时响应)
    public static final SipResponse TRYING = new SipResponse(100, "Trying");
    public static final SipResponse RINGING = new SipResponse(180, "Ringing");
    public static final SipResponse CALL_IS_BEING_FORWARDED = new SipResponse(181, "Call Is Being Forwarded");
    public static final SipResponse QUEUED = new SipResponse(182, "Queued");
    public static final SipResponse SESSION_PROGRESS = new SipResponse(183, "Session Progress");
    
    // 2xx Success (成功响应)
    public static final SipResponse OK = new SipResponse(200, "OK");
    public static final SipResponse ACCEPTED = new SipResponse(202, "Accepted");
    
    // 3xx Redirection (重定向响应)
    public static final SipResponse MULTIPLE_CHOICES = new SipResponse(300, "Multiple Choices");
    public static final SipResponse MOVED_PERMANENTLY = new SipResponse(301, "Moved Permanently");
    public static final SipResponse MOVED_TEMPORARILY = new SipResponse(302, "Moved Temporarily");
    public static final SipResponse USE_PROXY = new SipResponse(305, "Use Proxy");
    public static final SipResponse ALTERNATIVE_SERVICE = new SipResponse(380, "Alternative Service");
    
    // 4xx Client Error (客户端错误)
    public static final SipResponse BAD_REQUEST = new SipResponse(400, "Bad Request");
    public static final SipResponse UNAUTHORIZED = new SipResponse(401, "Unauthorized");
    public static final SipResponse PAYMENT_REQUIRED = new SipResponse(402, "Payment Required");
    public static final SipResponse FORBIDDEN = new SipResponse(403, "Forbidden");
    public static final SipResponse NOT_FOUND = new SipResponse(404, "Not Found");
    public static final SipResponse METHOD_NOT_ALLOWED = new SipResponse(405, "Method Not Allowed");
    public static final SipResponse NOT_ACCEPTABLE = new SipResponse(406, "Not Acceptable");
    public static final SipResponse PROXY_AUTHENTICATION_REQUIRED = new SipResponse(407, "Proxy Authentication Required");
    public static final SipResponse REQUEST_TIMEOUT = new SipResponse(408, "Request Timeout");
    public static final SipResponse GONE = new SipResponse(410, "Gone");
    public static final SipResponse REQUEST_ENTITY_TOO_LARGE = new SipResponse(413, "Request Entity Too Large");
    public static final SipResponse REQUEST_URI_TOO_LONG = new SipResponse(414, "Request-URI Too Long");
    public static final SipResponse UNSUPPORTED_MEDIA_TYPE = new SipResponse(415, "Unsupported Media Type");
    public static final SipResponse UNSUPPORTED_URI_SCHEME = new SipResponse(416, "Unsupported URI Scheme");
    public static final SipResponse BAD_EXTENSION = new SipResponse(420, "Bad Extension");
    public static final SipResponse EXTENSION_REQUIRED = new SipResponse(421, "Extension Required");
    public static final SipResponse INTERVAL_TOO_BRIEF = new SipResponse(423, "Interval Too Brief");
    public static final SipResponse TEMPORARILY_UNAVAILABLE = new SipResponse(480, "Temporarily Unavailable");
    public static final SipResponse CALL_LEG_DOES_NOT_EXIST = new SipResponse(481, "Call Leg/Transaction Does Not Exist");
    public static final SipResponse LOOP_DETECTED = new SipResponse(482, "Loop Detected");
    public static final SipResponse TOO_MANY_HOPS = new SipResponse(483, "Too Many Hops");
    public static final SipResponse ADDRESS_INCOMPLETE = new SipResponse(484, "Address Incomplete");
    public static final SipResponse AMBIGUOUS = new SipResponse(485, "Ambiguous");
    public static final SipResponse BUSY_HERE = new SipResponse(486, "Busy Here");
    public static final SipResponse REQUEST_TERMINATED = new SipResponse(487, "Request Terminated");
    public static final SipResponse NOT_ACCEPTABLE_HERE = new SipResponse(488, "Not Acceptable Here");
    public static final SipResponse BAD_EVENT = new SipResponse(489, "Bad Event");
    public static final SipResponse REQUEST_PENDING = new SipResponse(491, "Request Pending");
    public static final SipResponse UNDECIPHERABLE = new SipResponse(493, "Undecipherable");
    
    // 5xx Server Error (服务器错误)
    public static final SipResponse INTERNAL_SERVER_ERROR = new SipResponse(500, "Internal Server Error");
    public static final SipResponse NOT_IMPLEMENTED = new SipResponse(501, "Not Implemented");
    public static final SipResponse BAD_GATEWAY = new SipResponse(502, "Bad Gateway");
    public static final SipResponse SERVICE_UNAVAILABLE = new SipResponse(503, "Service Unavailable");
    public static final SipResponse SERVER_TIMEOUT = new SipResponse(504, "Server Time-out");
    public static final SipResponse VERSION_NOT_SUPPORTED = new SipResponse(505, "Version Not Supported");
    public static final SipResponse MESSAGE_TOO_LARGE = new SipResponse(513, "Message Too Large");
    
    // 6xx Global Failure (全局失败)
    public static final SipResponse BUSY_EVERYWHERE = new SipResponse(600, "Busy Everywhere");
    public static final SipResponse DECLINE = new SipResponse(603, "Decline");
    public static final SipResponse DOES_NOT_EXIST_ANYWHERE = new SipResponse(604, "Does Not Exist Anywhere");
    public static final SipResponse NOT_ACCEPTABLE_ANYWHERE = new SipResponse(606, "Not Acceptable Anywhere");
    
    /**
     * 根据状态码创建响应
     */
    public static SipResponse createResponse(int statusCode) {
        String reasonPhrase = getReasonPhrase(statusCode);
        return new SipResponse(statusCode, reasonPhrase);
    }
    
    /**
     * 获取状态码对应的原因短语
     */
    private static String getReasonPhrase(int statusCode) {
        switch (statusCode) {
            // 1xx Provisional
            case 100: return "Trying";
            case 180: return "Ringing";
            case 181: return "Call Is Being Forwarded";
            case 182: return "Queued";
            case 183: return "Session Progress";
            
            // 2xx Success
            case 200: return "OK";
            case 202: return "Accepted";
            
            // 3xx Redirection
            case 300: return "Multiple Choices";
            case 301: return "Moved Permanently";
            case 302: return "Moved Temporarily";
            case 305: return "Use Proxy";
            case 380: return "Alternative Service";
            
            // 4xx Client Error
            case 400: return "Bad Request";
            case 401: return "Unauthorized";
            case 402: return "Payment Required";
            case 403: return "Forbidden";
            case 404: return "Not Found";
            case 405: return "Method Not Allowed";
            case 406: return "Not Acceptable";
            case 407: return "Proxy Authentication Required";
            case 408: return "Request Timeout";
            case 410: return "Gone";
            case 413: return "Request Entity Too Large";
            case 414: return "Request-URI Too Long";
            case 415: return "Unsupported Media Type";
            case 416: return "Unsupported URI Scheme";
            case 420: return "Bad Extension";
            case 421: return "Extension Required";
            case 423: return "Interval Too Brief";
            case 480: return "Temporarily Unavailable";
            case 481: return "Call Leg/Transaction Does Not Exist";
            case 482: return "Loop Detected";
            case 483: return "Too Many Hops";
            case 484: return "Address Incomplete";
            case 485: return "Ambiguous";
            case 486: return "Busy Here";
            case 487: return "Request Terminated";
            case 488: return "Not Acceptable Here";
            case 489: return "Bad Event";
            case 491: return "Request Pending";
            case 493: return "Undecipherable";
            
            // 5xx Server Error
            case 500: return "Internal Server Error";
            case 501: return "Not Implemented";
            case 502: return "Bad Gateway";
            case 503: return "Service Unavailable";
            case 504: return "Server Time-out";
            case 505: return "Version Not Supported";
            case 513: return "Message Too Large";
            
            // 6xx Global Failure
            case 600: return "Busy Everywhere";
            case 603: return "Decline";
            case 604: return "Does Not Exist Anywhere";
            case 606: return "Not Acceptable Anywhere";
            
            default: return "Unknown";
        }
    }
}
